package algorithm.practice;
import java.util.Scanner;

/**
 * <ul>
 * <li>Title: BinaryTreeTest</li>
 * <li>Description: 基础的二叉树练习 </li>
 * </ul>
 *
 * @author swc
 * @date 2021-08-26 10:25
 */
public class BinaryTreeTest {



    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入节点的数量");
        int nodeCount  = sc.nextInt();

        TreeNode root = null;
        for (int i = 0; i < nodeCount; i++) {
            System.out.println("请输入值");
            root = createTreeByParentNode(sc.nextInt(),root);
        }


        System.out.println("----");
    }

    /**
     * 功能描述: 创建二叉树
     * @param value
     * @param parentNode
     * @return: algorithm.practice.Test.TreeNode
     * @author: swc
     * @date: 2021-08-26 10:22
     */
    private static TreeNode createTreeByParentNode(int value, TreeNode parentNode) {

        if(parentNode==null){
            parentNode = new TreeNode(value);
        }
        //当前值大于父节点 挂在右侧
        else if(parentNode.getVal()<value){
            if(parentNode.getRight()!=null){
                createTreeByParentNode(value,parentNode.getRight());
            }else{
                parentNode.setRight(new TreeNode(value));
            }
        }else{
            if(parentNode.getLeft()!=null){
                createTreeByParentNode(value,parentNode.getLeft());
            }else{
                parentNode.setLeft(new TreeNode(value));
            }
        }
        return  parentNode;
    }
}
